<?php


namespace app\api\controller\v2;


use app\admin\model\Admin;
use app\admin\model\cms\GrapevineModel;
use app\common\controller\Api;
use think\Config;

/**
 * 小道消息
 * Class Grapevine
 * @package app\api\controller\v2
 */
class Grapevine  extends Api
{
    //无需登录的方法,同时也就不需要鉴权了
//    protected $noNeedLogin = ['login', 'mobilelogin'];
    //无需鉴权的方法,但需要登录
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    //文章列表
    public function index(){
        $request = $this->request->param();
        $page = $request['page'] ?? 1;//页码
        $limit = $request['limit'] ?? 20;//每页数量
        $keyword = $request['keyword'] ?? '';//关键词
        $orderway = $request['orderway'] ?? 'desc';//排序方式:asc,desc

        $model = GrapevineModel::where('status',1)
            ->where('is_show',1)
            ->order('weigh','desc')
            ->order('id','desc')
            ->paginate($limit, false, ['page' => $page]);

        // 获取分页数据
        $data = $model->toArray();

        foreach($data['data'] as &$v){
            $v['createtime'] = date('Y-m-d H:i',$v['createtime']);
            $v['updatetime'] = date('Y-m-d H:i',$v['updatetime']);
        }

        $res = [
            'total'=>$data['total'],
            'list'=>$data['data'],
        ];
        $this->success('',$res);
    }

    //详情
    public function show(){
        $request = $this->request->param();
        $id = $request['id'] ?? '';
        if (!$id){
            $this->error('缺少id');
        }
        $model = GrapevineModel::where('id',$id)
            ->where('is_show',1)
            ->where('status',1)
            ->find();
        if (!$model){
            $this->error('文章不存在');
        }
        //获取管理员信息
        $admin = Admin::where('id',1)->field('id,nickname,avatar')->find();
        $res['plateInfo'] = [
            'id' => $model['id'],
            'title' => $model['title'],//帖子标题
            'user'=>[
              'nickname'=>$admin['nickname'],
              'avatar'=>obsurl($admin['avatar']),
            ],
            'content' => $model['content'],//帖子内容(富文本)
            'create_time' => $model['createtime'] ? date('Y-m-d H:i',$model['createtime']) : '',//创建时间
        ];
       $this->success('',$res);
    }
}